<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>WebSocket stomp + 广播式</title>
</head>
<body onload="setConnected(false)">
<noscript>
    <h2 style="color:#ff0000">貌似你的浏览器不支持websocket</h2>
</noscript>
<div>
    <div id="localinfo"></div>
    <div>
        <br><label for="wsurl">输入WS地址</label>
        <input type="text" style="width: 300px" id="wsurl" value="ws://127.0.0.1:8080/chat?user=demo"><br>
        <button id="connect" onclick="connect()">连接</button>
        <button id="disconnect" onclick="disconnect();">断开连接</button>
    </div>
    <div id="conversationDiv" style="visibility: hidden;">
        <label for="message">输入消息</label> <input type="text" id="message">
        <button id="send" onclick="send();">发给订阅者</button>
        <button id="send2" onclick="send2();">发给用户</button>
        <button id="send3" onclick="send3();">发给应用</button>
        <button id="send4" onclick="send4();">异常测试</button>
        <br>
        <p id="response"
           style="max-width:1498px;min-height:350px;max-height:350px;border:1px solid;overflow-y:auto;"></p>
    </div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/stomp.umd.min-5.4.4.js"></script>
<script type="text/javascript">
    var stompClient = null;

    function setConnected(connected) {
        document.getElementById('connect').disabled = connected;
        document.getElementById('disconnect').disabled = !connected;
        document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
        $('#response').html();
    }

    function connect() {
        //https://stomp-js.github.io/api-docs/latest/classes/Client.html
        stompClient = new StompJs.Client({
            onConnect: function (frame) {
                setConnected(true);

                stompClient.subscribe('/topic/todoTask1/*', function (response) {
                    showResponse(response.body);
                }, {ack: 'client'});

                stompClient.subscribe('/user/app/error', function (response) {
                    showResponse(response.body);
                }, {ack: 'client'});
            },
            onWebSocketClose: function (frame) {
                setConnected(false);
            },
            brokerURL: $('#wsurl').val()
        });

        stompClient.activate();
    }

    function disconnect() {
        if (stompClient != null) {
            stompClient.deactivate();
        }
        stompClient = null;
        setConnected(false);
    }

    function send() {
        if($('#message').val()){
            stompClient.publish({
                destination: "/topic/todoTask1/open",
                body: $('#message').val(),
                headers: {"openPersistence": "true", "openAck": "true"}
            });
        }
    }

    function send2() {
        if($('#message').val()){
            stompClient.publish({
                destination: "/app/todoTask1/user",
                body: $('#message').val(),
                headers: {"openPersistence": "true", "openAck": "true", "user": "test001"}
            });
        }
    }

    function send3() {
        if($('#message').val()){
            stompClient.publish({
                destination: "/app/todoTask1/self",
                body: $('#message').val(),
                headers: {"openPersistence": "true", "openAck": "true"}
            });
        }
    }

    function send4() {
        if($('#message').val()){
            stompClient.publish({
                destination: "/app/todoTask1/error",
                body: $('#message').val(),
                headers: {"openPersistence": "true", "openAck": "true"}
            });
        }
    }

    function showResponse(message) {
        var response = $('#response');
        response.append(message + "</br>");
        var div = document.getElementById('response');
        div.scrollTop = div.scrollHeight;
    }
</script>

</body>
</html>